<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests\AdminloginRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CollectController extends Controller
{
   /*
	加入收藏夹
    */
    public function getInsert(Request $request)
    {
        // 提取数据
        // dd($request->all());
        $data = $request->only(['goods_id']);
        //查询用户是否登录
        if(!empty(session('id'))){
            //如果登录
            $data['uid'] = session('id');
            //查询是否存在
            $res1 = DB::table('collect')->where('goods_id',$request->only(['goods_id']))->where('uid',session('id'))->get();
            if($res1){
                //存在,返回
                echo 3;
            }else{
                //不存在,添加
                $res = DB::table('collect')->insert($data);
                //判断添加是否成功
                if($res){
                    echo 1;
                }else{
                    echo 2;
                }
            }
        }else{
            //未登录,请登录
            echo 4;
        }
    }
    //收藏夹列表
    public function getIndex()
    {
        //查询该用户收藏夹里面的内容
        $collects = DB::table('goods')
                    ->join('collect','goods.id','=','collect.goods_id')
                    ->where('collect.uid',session('id'))
                    ->get();
        //如果有
        if($collects){
            $ress = 1;
        }else{
            $ress = 0;
        }
        // dd($collects);
        return view('home.collect.index',['collects'=>$collects],['ress'=>$ress]);
    }
    //收藏夹单个物品删除
    public function postDelete(Request $request)
    {
        $id = $request->input('id');
        $res = DB::table('collect')->where('id',$id)->delete();
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }
    //收藏夹所有物品删除
    public function postDeleteall()
    {
        $uid = session('id');
        $res = DB::table('collect')->where('uid',$uid)->delete();
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }

}
